<!--公司节假日设置-->
<template>
  <div class="m-window">
      <div class="m-condition">
        <el-row :gutter="5">
          <el-form
            ref="headerRuleForm"
            label-width="70px"
            :inline-message="true"
            label-position="left"
            class="m-headerForm"
          >
            <el-col :span="6">
              <el-form-item label="公司" class="rightItem">
                <el-select
                  placeholder="请选择公司"
                  v-model="companyCode"
                  clearable>
                    <el-option
                      v-for="(item,index) in companyItem"
                      :key="index"
                      :label="item.companyName"
                      :value="item.companyCode">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="2" :offset="12">
              <el-button @click="queryHolidaySetting" type="primary" :disabled="queryDisabled">查询</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="addHolidaySetting" type="primary" :disabled="addDisabled">新增</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="editHolidaySetting" type="primary" :disabled="editDisabled">编辑</el-button>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <!-- 表格数据 -->
      <div class="m-agTable m-bottom m-holiday-table">
        <ag-grid-vue class="ag-theme-balham ag-table"
            :gridOptions="gridOptions"
            :columnDefs="columnDefs"
            :rowData="rowData"
            v-loading="loading"
            @rowDoubleClicked="showDialog">
        </ag-grid-vue>
      </div>
      <!-- 弹出框 -->
      <el-dialog 
        :title="dialogTitle" 
        :visible.sync="dialogFormVisible" 
        v-dialogDrag>
        <el-form :model="holidayform" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="假期开始日期">
                <el-date-picker
                  v-model="holidayform.startDate"
                  type="date"
                  placeholder="选择开始日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="假期结束日期" >
                <el-date-picker
                  v-model="holidayform.endDate"
                  type="date"
                  placeholder="选择结束日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="状态" >
                <el-select v-model="holidayform.state" placeholder="请选择状态" clearable >
                  <el-option 
                  v-for="(item,index) in stateItem"
                  :key="index"
                  :value="item.code"
                  :label="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公司" >
                <el-select v-model="holidayform.companyCode" placeholder="请选择公司" clearable>
                  <el-option 
                  v-for="(item,index) in companyItem"
                  :key="index"
                  :value="item.companyCode"
                  :label="item.companyName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogCancel">取 消</el-button>
          <el-button type="primary" @click="dialogSubmit">保 存</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>

import gridOptions from "lesso-common/public/gridOptions"
import constant from "@/public/constant"

export default {
    name:"companyHolidaySetting",
    data(){
      return {

        queryDisabled:false,            //查询按钮可编辑
        addDisabled:false,              //新增按钮可编辑
        editDisabled:false,             //编辑按钮可编辑
        authInfo:null,
        companyCode:"",                 //公司编码
        companyItem:[],                 //公司
        companyName:"",                 //公司名称
        // 列表数据
        gridOptions:null,
        columnDefs:null,
        rowData:[],                     //表格数据
        loading:false,                  //加载

        //弹出框
        dialogTitle:"新增",             //标题             
        dialogFormVisible:false,        //弹出框隐藏
        holidayform:{                   //节假日表格数据
          startDate:"",                 //开始日期
          endDate:"",                   //结束日期
          state:"",                     //状态
          companyCode:""                //公司代码
        },
        stateItem:[                     //状态数据
          {code:0,value:"正常"},
          {code:1,value:"禁用"},
        ]
      }
    },

    beforeMount(){
      this.gridOptions = gridOptions()
      this.columnDefs=[
        {headerName:"公司代码",field:"companyCode",width:130,checkboxSelection: true,headerCheckboxSelection: true},
        {headerName:"假期开始日期",field:"startDate",width:180},
        {headerName:"假期结束日期",field:"endDate",width:180},
        {headerName:"状态",field:"state",width:130,
        cellRenderer:params=>{
         for(let i=0; i<this.stateItem.length;i++){
           let item=this.stateItem[i];
           if(params.value==item.code){
             return params.value=item.value
           }
         }
        }},
        {headerName:"公司名称",field:"companyName",width:230}
      ]

    },
    created(){
      this.searchCompany () 
      this.queryHolidaySetting();
      
    },
    methods:{
        //公司查询
        searchCompany () {
          var obj = {
            state: 0
          };
          var that =this;
          this.$http.getCompanyInfo(obj).then(function (response) {
            var data = response.data;
            if (data.code == "1" && data.total>0) {
              let companyData=data.rows.splice(0);
              that.companyItem=companyData;
            } else {
              that.$alert(data.info, {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            }
          }).catch((error) => {
            that.$alert("查询公司失败", {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            });
          })
      },

      //查询首页
      queryHolidaySetting(){
        let params ={};
        if(this.companyCode!==""){
          params={
            eqParams:{company_code:this.companyCode}
          }
         
        }
        this.loading=true;
        this.queryDisabled=true;
        this.$http.queryHolidaySetting(params)
        .then((res)=>{
          this.loading=false;
          this.queryDisabled=false;
          let data=res.data;
          if(data.code==1){
            let originData = data.rows.splice(0);
            this.rowData=originData;
          }else{
            this.rowData=[];
            this.$message({
              $message:"查询失败",
              type:"error"
            })
          }
        })
        .catch((err)=>{
          this.loading=false;
          this.queryDisabled=false;
          this.$alert("查询失败，请检查网络！", {
          confirmButtonText: "确定",
          customClass: "alertFailure"
          });
        })

      },

      //新增
      addHolidaySetting(){
        this.dialogFormVisible=true;
        this.dialogTitle="新增"
      },

      //编辑
      editHolidaySetting(){
        let selectRow=this.gridOptions.api.getSelectedRows();
        if(selectRow.length==1){
          this.dialogFormVisible=true;
          this.dialogTitle="编辑"
          this.holidayform={...selectRow[0]};
        }else if(selectRow.length>1){
          this.$alert("只能选择一行数据编辑",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }else{
          this.$alert("请选择要编辑的数据",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }
      },

      // 弹框取消
      dialogCancel(){
        this.holidayform={};
        this.dialogFormVisible=false;
      },
      //双击跳出编辑弹框
      showDialog(event){
        this.dialogFormVisible=true;
        let selectRow=event.data;
        this.dialogFormVisible=true;
        this.dialogTitle="编辑"
        this.holidayform={...selectRow};
      },
      // 弹框确认
      dialogSubmit(){
        let msg="";
        switch(true){ 
            case this.holidayform.startDate=="":
                msg="请选择假期开始日期";
                break;
            case this.holidayform.endDate=="":
                msg="请选择假期结束日期";
                break;
            case this.holidayform.startDate>this.holidayform.endDate :
                msg="开始时间必须小于结束时间"
                break;
            case /^[01]$/.test(this.holidayform.state)==false:
                msg="请选择状态";
                break;
            case this.holidayform.companyCode=="":
                msg="请选择公司";
                break;
           
        }
      
        if(msg!=""){
            this.$alert("提示:"+msg, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
            });
            return;
        }
        let params={
            companyCode: this.holidayform.companyCode,     //公司代码
            creator: this.$global.user.loginName,          //创建人
            creatorId: this.$global.user.id,               //创建人Id
            endDate:this.holidayform.endDate,              //结束日期
            startDate: this.holidayform.startDate,         //开始日期
            state: this.holidayform.state                  //状态
          }
        //根据公司代码带出公司名称
        this.companyItem.forEach(item=>{
          if(this.holidayform.companyCode==item.companyCode){
            params.companyName=item.companyName
          }
          });
        if(this.dialogTitle=="新增"){
          this.addDisabled=true;
          this.dialogFormVisible=true;
          this.$http.addHolidaySetting(params)
          .then(res=>{
            this.addDisabled=false;
            this.dialogFormVisible=false;
            let data = res.data;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
            })
            this.holidayform={};
            this.queryHolidaySetting();
            
            }else{
              this.$alert(`提示:${data.info}`, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
              });
            }
          })
          .catch(error=>{
              this.addDisabled=false;
              this.dialogFormVisible=false;
              this.$alert("新增失败，请检查网络！", {
                confirmButtonText: "确定",
                customClass: "alertFailure"
              });
          })
        }else if(this.dialogTitle=="编辑"){
          this.editDisabled=true;
          params.id=this.holidayform.id                    //编辑ID
          params.creatorId=this.holidayform.creatorId      //创建人ID
          params.creator=this.holidayform.creator,         //创建人
          params.modifyer=this.$global.user.loginName,     //修改人
          params.modifyerId=this.$global.user.id,          //修改人ID
          //根据公司代码带出公司名称
          this.companyItem.forEach(item=>{
            if(this.holidayform.companyCode==item.companyCode){
              params.companyName=item.companyName
            }
          });
          this.$http.editHolidaySetting(params).then(res=>{
            this.editDisabled=false;
            this.dialogFormVisible=false;
            let data = res.data;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
            })
            this.holidayform={};
            this.queryHolidaySetting();
            }else{
              this.holidayform={};
              this.$alert(`提示:${data.info}`, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
              });
            }
          }).catch(error=>{
            this.editDisabled=false;
            this.dialogFormVisible=false;
            this.holidayform={};
            this.$alert(error.data.info, {
              confirmButtonText: "确定",
              customClass: "alertFailure"
            });
          })
          
        }
        
      },
    }
}
</script>

<style lang="less" scoped>
  
</style>